package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.api.cache.prefix.ScenicPrefix;
import cn.wolfcode.luowowo.api.cache.service.IScenicStatisRedisService;
import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.service.IDestinatonService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.domain.ScenicCommentTag;
import cn.wolfcode.luowowo.comment.domain.ScenicReply;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.comment.service.IScenicCommentTagService;
import cn.wolfcode.luowowo.comment.service.IScenicReplyService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.scenic.domain.Scenic;
import cn.wolfcode.luowowo.scenic.domain.ScenicTheme;
import cn.wolfcode.luowowo.scenic.query.ScenicCommentQueryObject;
import cn.wolfcode.luowowo.scenic.query.ScenicQueryObject;
import cn.wolfcode.luowowo.scenic.query.ScenicReplyQueryObject;
import cn.wolfcode.luowowo.scenic.service.IScenicService;
import cn.wolfcode.luowowo.scenic.service.IScenicThemeService;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 景点
 */
@Controller
@RequestMapping("/scenic")
public class ScenicController {

    @Reference
    private IScenicService scenicService;
    @Reference
    private IScenicCommentService scenicCommentService;
    @Reference
    private IScenicReplyService scenicReplyService;
    @Reference
    private IScenicCommentTagService scenicCommentTagService;
    @Reference
    private IScenicStatisRedisService scenicStatisRedisService;
    @Reference
    private IScenicThemeService scenicThemeService;
    @Reference
    private IDestinatonService destinatonService;

    @RequestMapping("/index")
    public String index(Long id, Model model) {
        // 获取吐司, 目的地
        List<Destination> toasts = destinatonService.getToasts(id);
        model.addAttribute("dest", toasts.get(toasts.size()-1));
        toasts.remove(toasts.size()-1);
        model.addAttribute("toasts", toasts);
        // 获取推荐TOP5的景点
        List<Scenic> top5 = scenicService.getTop5(id);
        model.addAttribute("top5", top5);
        // 获取热门景点
        List<Scenic> hotCommend = scenicService.getHotCommed(id);
        model.addAttribute("hotCommend", hotCommend);
        // 获取景点主题
        List<ScenicTheme> scenicThemes = scenicThemeService.getScenicTheme(id);
        model.addAttribute("scenicTheme", scenicThemes);
        return "scenic/index";
    }

    @RequestMapping("/scenicthemeclassify")
    public String scenicThemeClassify(ScenicQueryObject qo, Model model){
        qo.setPageSize(5);
        PageInfo page = scenicService.queryByTheme(qo);
        model.addAttribute("page", page);
        return "scenic/scenicThemeClassifyTpl";
    }

    // 跳转显示景点详细界面
    @RequestMapping("/detail")
    public String detail(Long id, Model model) {
        Scenic scenic = scenicService.queryById(id);
        model.addAttribute("scenic", scenic);
        // 评论的各种数量
        model.addAttribute("tagEleCount", scenicCommentService.getCount(id));
        // 自定义标签的数量
        model.addAttribute("commentTags", scenicCommentTagService.getTop10(id));

        return "scenic/detail";
    }

    // 评论图片上传
    @Value("${file.path}")
    private String basePath;

    @RequestMapping("/image")
    @ResponseBody
    public Map upload(MultipartFile file) {
        HashMap<Object, Object> map = new HashMap<>();
        try {
            String url = UploadUtil.upload(file, basePath);
            map.put("status", 1);
            map.put("url", url);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("status", 0);
            map.put("msg", "上传失败");
        }
        return map;
    }

    //评论添加
    //@RequireLogin
    @RequestMapping("/commentAdd")
    @ResponseBody
    public Object commentAdd(ScenicComment comment, @UserParam UserInfo userInfo) {
        // 保存评论
        comment.setUserId(userInfo.getId());
        comment.setUsername(userInfo.getNickname());
        comment.setCity(userInfo.getCity());
        comment.setUserHeadUrl(userInfo.getHeadImgUrl());
        comment.setLevel(userInfo.getLevel());
        scenicCommentService.save(comment);
        // 将评论中的星星数添加到Redis中该景点的统计数据中
        scenicStatisRedisService.increaseScenicStatis(comment.getScenicId(), comment.getSynStarnum()
                , comment.getSceneryStarnum()
                , comment.getServiceStarnum()
                , comment.getFeatureStarnum());
        // 将Redis中该景点的评论数量+1
        scenicStatisRedisService.increaseScenicCommentStatis(comment.getScenicId());
        // 排行榜分值实时统计
        // 权宜之计, 先查出地区id, 之后再加评论的字段
        Scenic scenic = scenicService.queryById(comment.getScenicId());
        int top5Score = comment.getSynStarnum() + comment.getSceneryStarnum() + comment.getServiceStarnum() + comment.getFeatureStarnum();
        scenicStatisRedisService.addScenicCommendScore(ScenicPrefix.SCENIC_TOP5_PREFIX, scenic.getDest().getId() + "", ScenicPrefix.SCENIC_RANK_STATIS_PREFIX, comment.getScenicId(), top5Score);
        scenicStatisRedisService.addScenicCommendScore(ScenicPrefix.SCENIC_HOTCOMMEND_PREFIX, scenic.getDest().getId() + "",ScenicPrefix.SCENIC_RANK_STATIS_PREFIX, comment.getScenicId(), 1);

        return new AjaxResult();
    }


    //评论显示
    @RequestMapping("/comment")
    public String comment(Model model, @ModelAttribute("qo") ScenicCommentQueryObject qo) {
        // 分页查询
        qo.setPageSize(4);
        Page<ScenicComment> page = scenicCommentService.query(qo);
        model.addAttribute("page", page);
        return "scenic/commentTpl";
    }

    //评论回复添加
    //@RequireLogin
    @RequestMapping("/replyAdd")
    @ResponseBody
    public Object replyAdd(ScenicReply reply, @UserParam UserInfo userInfo) {
        reply.setUserId(userInfo.getId());
        reply.setUsername(userInfo.getNickname());
        reply.setUserHeadUrl(userInfo.getHeadImgUrl());
        scenicReplyService.save(reply);
        return new AjaxResult();
    }

    //评论回复显示
    @RequestMapping("/reply")
    public String reply(Model model, @ModelAttribute("qo") ScenicReplyQueryObject qo) {
        // 测试
        List<ScenicReply> list = scenicReplyService.getAll(qo);
        model.addAttribute("scenicReply", list);
        return "scenic/replyTpl";
    }

    // 手动添加评论主题
    @RequestMapping("/addTheme")
    @ResponseBody
    public Object addTheme() {
        ScenicCommentTag tag1 = new ScenicCommentTag();
        for (int i = 10; i >= 1; i--) {
            tag1.setScenicId(2L);
            tag1.setName("前排" + i);
            scenicCommentTagService.save(tag1);
        }
        return "成功添加十个";
    }

}
